<template>
    <div class="school-student-container">
        <el-card shadow="hover" class="mb10" v-auth="'school:student:list'">
            <el-form :inline="true" :model="search">
                <el-form-item>
                    <el-input v-model="search.name" placeholder="请输入名称" prefix-icon="el-icon-search"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="search.status" placeholder="请选择状态">
                        <el-option key="key1" label="关键值1" value="key1"></el-option>
                        <el-option key="key2" label="关键值2" value="key3"></el-option>
                        <el-option key="key3" label="关键值3" value="key3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-date-picker type="daterange" align="right" v-model="dateData.value" :shortcuts="dateData.shortcuts" 
                        unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
                    <el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card shadow="hover">
            <el-button v-auth="'school:student:add'" type="primary" class="ml10" icon="el-icon-document-add" @click="onOpenAddStudent()">新增</el-button>
            <el-button v-auth="'school:student:edit'" type="primary" class="ml10" icon="el-icon-edit" @click="onOpenEditStudent()" :disabled="tableData.selections.length != 1">修改</el-button>
            <el-button v-auth="'school:student:remove'" type="primary" class="ml10" icon="el-icon-delete" @click="onTabelRowsDel()" :disabled="tableData.selections.length <= 0">删除</el-button>
            <el-table ref="tableStudentRef" :data="tableData.data" @selection-change="onTableSelectionChange" style="width: 100%" row-key="id">
                <el-table-column type="selection" align="cneter" width="50" show-overflow-tooltip></el-table-column>
                <el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
                <el-table-column label="名称" prop="name" show-overflow-tooltip></el-table-column>
                <el-table-column label="创建者" prop="createBy" show-overflow-tooltip></el-table-column>
                <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip></el-table-column>
                <el-table-column label="状态" prop="status" show-overflow-tooltip></el-table-column>
                <el-table-column label="联系电话" prop="phone" show-overflow-tooltip></el-table-column>
                <el-table-column label="排序" prop="sort" show-overflow-tooltip></el-table-column>
                <el-table-column label="父级编号" prop="parentId" show-overflow-tooltip></el-table-column>
                <el-table-column label="祖级序列" prop="ancestors" show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" show-overflow-tooltip width="125" v-auths="['school:student:edit', 'school:student:remove']">
                    <template #default="scope">
                        <el-button v-auth="'school:student:edit'" type="primary" link @click="onOpenEditStudent(scope.row)">修改</el-button>
                        <el-button v-auth="'school:student:remove'" type="primary" link @click="onTabelRowDel(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                @size-change="onPageSizeChange"
                @current-change="onPageCurrentChange"
                class="mt15"
                :pager-count="5"
                :page-sizes="[10, 20, 30, 40, 50, 60]"
                v-model:current-page="search.pageNum"
                v-model:page-size="search.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="tableData.total"
            >
            </el-pagination>
        </el-card>
        <AddStudent ref="addStudentRef" @submit="onTableSearch"/>
        <EditStudent ref="editStudentRef" @submit="onTableSearch"/>
    </div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddStudent from '/@/views/school/student/addStudent.vue';
import EditStudent from '/@/views/school/student/editStudent.vue';
import { getStudentList, delStudent } from '/@/api/school/student/index';
import { formatDate, getDateRangeShortcuts } from '/@/utils/formatTime';
export default {
    name: 'school/student',
    components: { AddStudent, EditStudent },
    setup() {
        const tableStudentRef = ref();
        const addStudentRef = ref();
        const editStudentRef = ref();
        const state = reactive({
            search:{
                name: undefined,
                status: undefined,
                startTime: '',
                endTime: '',
                pageNum: 1,
                pageSize: 10,
            },
            dateData: {
                value: [],
                shortcuts: getDateRangeShortcuts()
            },
            tableData: {
                selections: [],//选中的行数据
                data: [],
                total: 0,
                loading: false
            }
        });

        const loadTableData = (params) => {
            getStudentList(Object.assign(state.search, params)).then((res) => {
                if(res.code == 0){
                    state.tableData.data = res.data;
                    state.tableData.total = res.total;
                    tableStudentRef.value.clearSelection();
                }
            });
        }

        //查询
        const onTableSearch = () => {
            if(state.dateData.value.length == 2){
                state.search.startTime = formatDate(state.dateData.value[0], 'yyyy-MM-dd') + ' 00:00:00';
                state.search.endTime = formatDate(state.dateData.value[1], 'yyyy-MM-dd') + ' 23:59:59';
            }
            loadTableData(state.search);
        }
        //重置
        const onResetSearch = () => {
            state.dateData.value = [];
            state.search.name = undefined;
            state.search.status = undefined;
            state.search.startTime = undefined;
            state.search.endTime = undefined;
            state.search.pageNum = 1;
            state.search.pageSize = 10;
            loadTableData(state.search);
        }
        // 监听表格选中行
        const onTableSelectionChange = (selections) =>{
            state.tableData.selections = selections;
        };
        // 每页显示数据大小改变
        const onPageSizeChange = (val: number) => {
            state.search.pageSize = val;
            loadTableData(state.search);
        };
        // 页码改变
        const onPageCurrentChange = (val: number) => {
            state.search.pageNum = val;
            loadTableData(state.search);
        };
        // 打开新增弹窗
        const onOpenAddStudent = (row: object) => {
            addStudentRef.value.openDialog(row);
        };
        // 打开编辑弹窗
        const onOpenEditStudent = (row: object) => {
            row = row || state.tableData.selections[0];
            editStudentRef.value.openDialog(row);
        };
        // 删除选中行
        const onTabelRowsDel = () => {
            const ids = state.tableData.selections.map(item => item.id);
            if(ids && ids.length > 0){
                ElMessageBox.confirm('确定删除选中的学生信息信息吗？', '提示', {
                    confirmButtonText: '删除',
                    cancelButtonText: '取消',
                    type: 'warning',
                }).then(() => {
                    delStudent(ids).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('删除成功');
                            loadTableData();
                        }
                    });
                }).catch(() => {});
            }
        };
        // 删除当前行
        const onTabelRowDel = (row: object) => {
            ElMessageBox.confirm('确定删除该条学生信息信息吗？', '提示', {
                confirmButtonText: '删除',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                delStudent([row.id]).then((res) => {
                    if(res.code === 0){
                        ElMessage.success('删除成功');
                        loadTableData();
                    }
                });
            }).catch(() => {});
        };
        // 页面加载时
        onMounted(() => {
            loadTableData();
        });
        return {
            tableStudentRef,
            addStudentRef,
            editStudentRef,
            onTableSearch,
            onResetSearch,
            onTableSelectionChange,
            onPageSizeChange,
            onPageCurrentChange,
            onOpenAddStudent,
            onOpenEditStudent,
            onTabelRowsDel,
            onTabelRowDel,
            ...toRefs(state),
        };
    },
};
</script>